package com.lastingwar.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lastingwar.mall.product.entity.AttrAttrgroupRelationEntity;
import com.lastingwar.mall.product.entity.AttrEntity;
import com.lastingwar.mall.product.service.AttrAttrgroupRelationService;
import com.lastingwar.mall.product.service.AttrService;
import com.lastingwar.mall.product.service.CategoryService;
import com.lastingwar.mall.product.vo.AttrGroupRelationVo;
import com.lastingwar.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lastingwar.mall.product.entity.AttrGroupEntity;
import com.lastingwar.mall.product.service.AttrGroupService;
import com.lastingwar.common.utils.PageUtils;
import com.lastingwar.common.utils.R;



/**
 * 属性分组
 *
 * @author yhm
 * @email 403627000@qq.com
 * @date 2020-05-29 16:37:08
 */
@RestController
    @RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService relationService;

    /**
     * 处理响应/product/attrgroup/attr/relation
     * @param vos 添加管理的列表
     * @return R.ok()
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){

        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     *  对应响应 product/attrgroup/{catelogId}/withattr
     *  获取分类下所有分组&关联属性
     * @param catelogId 分类ID
     * @return 分类下的分组和属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",vos);
    }

    /**
     * 处理响应 product/attrgroup/{attrgroupId}/attr/relation
     * 返回分组对应的属性
     */

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities =  attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }
    /**
     * 完成响应product/attrgroup/attr/relation/delete，删除对应的关联
     * @param vos 请求的voAttrGroupRelationVo数组
     * @return R.ok
     */

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody  AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }


    /**
     * 响应product/attrgroup/{attrgroupId}/noattr/relation
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     * @param attrgroupId 属性分组ID
     * @param params  分页基本信息
     * @return  当前分组未关联属性的分页内容
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId)    {
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		//分组所属分类的ID
        Long catelogId = attrGroup.getCatelogId();
        //使用名称为findCatelogPath的方法找到全部的路径ID
        Long[] path =categoryService.findCatelogPath(catelogId);
        //将路径添加进入
        attrGroup.setCatalogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
